<template>
  <div>
    <Row type="flex" :gutter="8" style="margin:15px 0;">
      <Col
        ><Button
          type="primary"
          ghost
          @click="autoProduceQuestion"
          v-if="!allQuestionList.length"
          :disabled="(competeStatus !== '0') || notOwnCompetition"
          >一键生成题目</Button
        ></Col
      >
      <Col
        ><Button
          type="primary"
          ghost
          @click="adjustQuestion"
          v-if="allQuestionList.length"
          :disabled="(competeStatus !== '0') || notOwnCompetition"
          >调整题目</Button
        ></Col
      >
      <Col
        ><Button
          type="primary"
          ghost
          @click="singleModifyQuestion"
          v-if="!allQuestionList.length"
          :disabled="(competeStatus !== '0') || notOwnCompetition"
          >单个添加题目</Button
        ></Col
      >
    </Row>
    <Row>
      <Col span="24" style="margin-top: 20px;border-top: 1px solid #aaa;padding-top: 10px">
        <QuestionList :data="allQuestionList">
          <!-- :showActions="true" -->
          <!-- <template slot="action" slot-scope="{ question, index }">
                <Poptip
                    confirm
                    transfer
                    title="确认删除吗?"
                    @on-ok="deleteQuestion([question.id])"
                    :disabled="
                        (competeStatus !== '0') || notOwnCompetition
                    "
                >
                    <Button type="error" size="small"><Icon type="ios-trash"/></Button>
                </Poptip>
            </template> -->
        </QuestionList>
        <template v-if="total > 0">
          <!-- <QuestionList :data="allQuestionList"></QuestionList> -->
          <Page
            :total="total"
            show-total
            :current="queryObj.page"
            :page-size="queryObj.rows"
            @on-page-size-change="queryAllQuestionSize"
            @on-change="queryAllQuestionPage"
            class="flex-center"
          />
        </template>
        <!-- <div v-if="allQuestionList.length===0"><Tag color="volcano">暂无题目，请先添加</Tag></div>
        <question-list :allQuestionList="allQuestionList"
                       :totals = "total"
                       :showPage = true
                       ref="questionList"
                       :showScore="true"
                       @queryListPage="queryAllQuestionPage"
                       @queryListSize="queryAllQuestionSize"></question-list> -->
      </Col>
    </Row>
    <question-model ref="questionModels" @queryList="queryAllQuestionPage(1)"></question-model>
  </div>
</template>

<script>
// 随便提交点东西，触发发版
import { mapGetters, mapState, mapMutations } from 'vuex'
import encryption from '@/libs/encryption.js'
import usally from '@/mixin/usally'
import questionModel from './questionModel'
import QuestionList from '@/components/question-list'
import { queryAllKnowLedgeQuestion } from '@/api/knowledgeCompet'
export default {
  name: 'componentQuestion',
  mixins: [usally],
  components: { questionModel, QuestionList },
  inject: ['detailParent'],
  data() {
    let September = sessionStorage.getItem('November')
    September = JSON.parse(encryption.SelfUseDecrypt(September))
    return {
      USER_ID: September.userId,
      //routeParams:{},
      queryObj: {
        page: 1,
        rows: 10
      },
      allQuestionList: [],
      total: 0
    }
  },
  computed: {
    competeDetail(){
      return this.detailParent.competeDetail
    },
    createUserId(){
      return this.competeDetail.createUserId
    },
    notOwnCompetition() {
      return this.competeDetail.notOwnCompetition
    },
    competeStatus(){
      return this.competeDetail.activityStatus
    },
    setHasQues(){
      return this.detailParent.setHasQues
    }
  },
  mounted() {
    this.queryAllQuestion()
  },
  methods: {
    autoProduceQuestion() {
      this.$refs.questionModels.singleAddQuestion(true, 2, 'auto')
    },
    singleModifyQuestion() {
      this.$refs.questionModels.singleAddQuestion(true, 1, 'single')
    },
    adjustQuestion() {
      this.$refs.questionModels.singleAddQuestion(true, 1, 'modify')
    },
    queryAllQuestion() {
      let _this = this
      queryAllKnowLedgeQuestion(this.competeDetail.id, this.queryObj)
        .then((data) => {
          if (data.code === 1) {
            let ques = data.attribute.dataList
            ques.forEach((item) => {
              if (item.keAnswerTOS) {
                item.answers = _this.sortQuestionAnswer(item.keAnswerTOS)
                item.name = item.description
              }
            })
            _this.allQuestionList = ques
            _this.total = data.attribute.totalCount
            if (_this.total > 0) {
              this.setHasQues(true)
            } else {
              this.setHasQues(false)
            }
          } else {
            this.setHasQues(false)
          }
        })
        .catch((error) => {
          this.setHasQues(false)
        })
    },
    noFilter(val) {
      let num = val + 1
      let str
      switch (num) {
        case 1:
          str = 'A'
          break
        case 2:
          str = 'B'
          break
        case 3:
          str = 'C'
          break
        case 4:
          str = 'D'
          break
        case 5:
          str = 'E'
          break
        case 6:
          str = 'F'
          break
        case 7:
          str = 'G'
          break
        case 8:
          str = 'H'
          break
        case 9:
          str = 'I'
          break
        case 10:
          str = 'J'
          break
        case 11:
          str = 'K'
          break
        case 12:
          str = 'L'
          break
        default:
          str = ''
          break
      }
      return str
    },
    queryAllQuestionSize(size) {
      this.queryObj.rows = size
      this.queryAllQuestion()
    },
    queryAllQuestionPage(page) {
      this.queryObj.page = page
      // this.$refs.questionList.page = page
      this.queryAllQuestion()
    },
    resetQuery() {
      //父组件切换调用
      this.queryObj = Object.assign(this.queryObj, {
        page: 1,
        rows: 10
      })
      // this.$refs.questionList.setPage(1)
      // this.$refs.questionList.setRows(10)
      this.queryAllQuestion()
    }
  }
}
</script>

<style scoped></style>
